package interfaces.clients.storage;


import java.io.*;

/**
 * Created by Chwan on the 29.11.2006 at 0:11:27
 */
public class ExtendedFile extends File {
	public ExtendedFile(String pathname) {
		super(pathname);
	}

	public ExtendedFile(String parent, String child) {
		super(parent, child);
	}

	public ExtendedFile(File parent, String child) {
		super(parent, child);
	}

	public ExtendedFile[] listExtendedFiles() {
		String[] ss = list();
		if(ss == null) return null;
		int n = ss.length;
		ExtendedFile[] fs = new ExtendedFile[n];
		for(int i = 0; i < n; i++) {
			fs[i] = new ExtendedFile(this, ss[i]);
		}
		return fs;
	}

	/**
	 * Writes <code>contents</code> to the <code>file</code>.
	 *
	 * @param contents String to be written
	 * @throws StorageIOException if the file exists but is a directory rather than
	 *                            a regular file, does not exist but cannot be created,
	 *                            or cannot be opened for any other reason
	 */
	public void write(String contents) throws StorageIOException {
		BufferedWriter writer;
		try {
			writer = new BufferedWriter(new FileWriter(this));
		} catch (IOException e) {
			throw new StorageIOException(this.getPath()+" has some IO problems!", e);
		}
		try {
			writer.write(contents);
			writer.flush();
		} catch (IOException e) {
			throw new StorageIOException(this.getPath()+" has some IO problems!", e);
		} finally {
			try {
				writer.close();
			} catch (IOException e) {
				throw new StorageIOException(this.getPath()+" has some IO problems!", e);
			}
		}
	}

	public String read() throws StorageFileNotFoundException, StorageIOException {
		BufferedReader reader;
		try {
			reader = new BufferedReader(new FileReader(this));
		} catch (FileNotFoundException e) {
			throw new StorageFileNotFoundException(this.getPath()+" not found!", e);
		}
		//Line-by-line reading from file
		String contents = "";
		try {
			while(reader.ready()) {
				contents = contents+reader.readLine()+"\n";
			}
		} catch (IOException e) {
			throw new StorageIOException(this.getPath()+" has some IO problems!", e);
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				throw new StorageIOException(this.getPath()+" has some IO problems!", e);
			}
		}

		return contents;
	}

	public void createFile() throws StorageIOException {
		if(!this.getParentFile().exists()) {
			this.getParentFile().mkdirs();
		}
		try {
			this.createNewFile();
		} catch (IOException e) {
			throw new StorageIOException(this.getPath()+" has some IO problems!", e);
		}

	}

	public boolean emptyDirectory() throws StorageFileNotFoundException, StorageIOException {
		if(!this.isDirectory()) return false;

		ExtendedFile l[] = this.listExtendedFiles();
		for(int i = 0; i < l.length; i++) {
			if(l[i].isDirectory()) {
				if(!l[i].deleteDirectory()) {
					System.out.println(l[i].getPath());
					return false;
				}
			} else if(!l[i].delete()) {
				System.out.println(l[i].getPath());
				return false;
			}
		}
		return true;
	}

	public boolean deleteDirectory() throws StorageFileNotFoundException, StorageIOException {
		if(!this.isDirectory()) return false;

		ExtendedFile l[] = this.listExtendedFiles();
		for(int i = 0; i < l.length; i++) {
			if(l[i].isDirectory()) {
				if(!l[i].deleteDirectory()) return false;
			} else if(!l[i].delete()) {
				return false;
			}
		}
		return this.delete();
	}
}
